package com.abc.core.util;

import com.abc.core.def.Dna;
import com.abc.core.def.Vd;
import com.abc.core.instance.bo.Cell;
import com.abc.core.instance.bo.Inst;
import com.abc.core.instance.bo.Va;
import com.abc.core.tool.DnaTool;
import com.fasterxml.jackson.databind.JsonNode;

public class CellTool {

    public static Inst jsonNode2Inst(JsonNode instNode, String instType, Dna dna) {
        String dnaCode = null;
        if (!instNode.get("dnaCode").isNull()) {
            dnaCode = instNode.get("dnaCode").asText();
        }
        String dnaName = null;
        if (!instNode.get("dnaName").isNull()) {
            dnaName = instNode.get("dnaName").asText();
        }

        int total = -1;
        if (!instNode.get("total").isNull()) {
            total = instNode.get("total").asInt();
        }

        Inst inst = new Inst(dna);
        inst.setTotal(total);

        JsonNode instChildNode = instNode.get("cells");
        if (instChildNode != null) {
            for (JsonNode cn : instChildNode) {
                inst.addCell(jsonNode2Cell(instNode, instType, dna));
            }
        }

        return inst;
    }

    private static Cell jsonNode2Cell(JsonNode cellNode, String instType, Dna dna) {
        Cell cell = initJsonNode2Cell(instType, cellNode, dna);
        for (Vd vd : dna.getVds()) {
            if (cellNode.get(vd.getVdName()) == null) {
                continue;
            }

            String vdName = vd.getVdName();
            Va va = cell.getVaByName(vdName);
            if (cellNode.get(vdName) != null) {
                setVa(cellNode.get(vdName), va, vd);
            }
        }

        for (Dna childDna : dna.getChildren()) {
            String childDnaName = childDna.getDnaName();
            if (cellNode.get(childDnaName) == null) {
                continue;
            }

            Inst childInst = jsonNode2Inst(cellNode.get(childDnaName), instType, childDna);
            cell.addChildInst(childDnaName, childDna.getDnaCode(), childInst);
        }

        if (dna.isCursive()) {
            String dnaName = dna.getDnaName();
            if (cellNode.get(dnaName) != null) {
                Inst childInst = jsonNode2Inst(cellNode.get(dnaName), instType, dna);
                cell.addChildInst(dnaName, dna.getDnaCode(), childInst);
            }
        }

        return cell;
    }

    private static Cell initJsonNode2Cell(String instType, JsonNode cellNode, Dna dna) {
        Cell cell = DnaTool.dna2Cell(instType, dna);
        Long id = 0L;
        if (cellNode.get("id") != null && !cellNode.get("id").isNull()) {
            id = cellNode.get("id").asLong();
        }

        String operationFlag = null;
        if (cellNode.get("operationFlag") != null && !cellNode.get("operationFlag").isNull()) {
            operationFlag = cellNode.get("operationFlag").asText();
        }
        Long rootId = 0L;
        if (cellNode.get("rootId") != null && !cellNode.get("rootId").isNull()) {
            rootId = cellNode.get("rootId").asLong();
        }

        Long parentId = 0L;
        if (cellNode.get("parentId") != null && !cellNode.get("parentId").isNull()) {
            parentId = cellNode.get("parentId").asLong();
        }

        cell.setId(id);
        cell.setRootId(rootId);
        cell.setParentId(parentId);
        cell.setOperationFlag(operationFlag);

        return cell;
    }

    private static void setVa(JsonNode jsonNode, Va va, Vd vd) {
        // TODO return null
    }

}
